Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmEditor
	Inherits System.Windows.Forms.Form 'Declares that all variables must be defined before use.
	
	'This example shows how to drag items in a listbox. For more info on commands see yellow vb book (Deitel & Deitel) page 424
	'Make sure the read only property  lstXSheet.MultiSelect = 2 .This sets the listbox to allow multiple selection and shift / control buttons (select between etc.)
	
	
	'This is a structure to hold the index data at the end of the AVI file it is based on a AVIOLDSTRUCTURE see MSDN website
	Private Structure INDEXSTRUCT
		Dim dwChunkid As Integer 'Holds the idx1 identifier
		Dim dwFlags As Integer 'Various flag data (always is set to decimal 16)
		Dim dwOffset As Integer 'The offset of the data from the start of the file
		Dim dwSize As Integer 'The size of the data chunk
	End Structure
	
	Dim IndexBlock As INDEXSTRUCT 'An instance of the above structure
	
	Dim IndexStartPoint As Integer 'The start point of the index file
	Dim ReadinData1 As Integer 'Data read from the file
	'Dim ReadinData2 As Long                                'Data read from the file
	'Dim ReadinData3 As Long                                'Data read from the file
	Dim MoviStart As Integer 'The byte where the "movi" header is
	Dim idx1Start As Integer 'The byte where th "idx1" index is
	Dim TotNumFrames As Integer 'The total number of frames in the file
	Dim FrameCounter As Integer 'The loop counter that counts through the frames as they are extracted
	Dim JpegFileName As String 'The filename of each new frame
	Dim RawJpegData() As Byte 'A byte array which holds the raw jpeg data taken from the avi file
	Dim FrameText As String 'A string containing frame number 000001, 000002 etc.
	Dim NumLinesinXSheet As Integer 'The number of visible lines in the X sheet
	Dim OriginalNumFrames As Integer 'The original number of frames in the file before editing
	Dim SourceFileNames() As String 'An array holding the source file names
	Dim NumSourceFiles As Integer 'The total number of source files the editor is using
	
	Dim FrameRate As Integer
	Dim ReturnCheck As Boolean
	Dim SourceFile As String
	'Dim DragIndex As Long
	
	Dim ReportReady As String
	
	Dim ErrorCode As Integer
	
	Dim ChangesMade As Boolean 'This is set to true only if the user has made any changes to the X sheet
	
	Dim EditorSliderLeft As Integer
	Dim EditorSliderTop As Integer
	Dim EditorSliderWidth As Integer
	Dim EditorSliderHeight As Integer
	Dim EditorSliderPosition As Integer
	Dim CurrentFrameFPS As Integer
	Dim LastFrameFPS As Integer
	Dim FirstFound As Integer 'The position in the list of the first selected item. This is used to reset the display in the cut and delete actions
	
	
	
	Private Sub frmEditor_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		
		EditorSliderLeft = 389
		EditorSliderTop = 504
		EditorSliderWidth = 279
		EditorSliderHeight = 15
		
		ErrorCode = 0
		'UPGRADE_NOTE: The Following line was commented to give the same effect as VB6. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="1C44C272-DA6E-4A2A-BDC5-9A27F0D03D31"'
		'Me.FormBorderStyle = 0
		'UPGRADE_ISSUE: Form property frmEditor.ScaleMode is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="8027179A-CB3B-45C0-9863-FAA1AF983B59"'
        'frmEditor.ScaleMode = 3 'Set scalemode to pixels
		Me.Width = VB6.TwipsToPixelsX(960 * VB6.TwipsPerPixelX)
		Me.Height = VB6.TwipsToPixelsY(710 * VB6.TwipsPerPixelY)
		Me.Left = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) / 2) - ((960 * VB6.TwipsPerPixelX) / 2)) 'Set the window to be in the middle of the screen
		Me.Top = VB6.TwipsToPixelsY((VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) / 2) - (((710 / 2) + 28) * VB6.TwipsPerPixelY)) 'Set the window raised up slightly. So it is better positioned when running at 1024 x 768
		
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'picFaciaEditor.ScaleMode = 3
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.AutoSize was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		picFaciaEditor.AutoSize = True : picFaciaEditor.Left = 0 : picFaciaEditor.Top = 0
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'picFaciaEditor.AutoRedraw = True : picFaciaEditor.BorderStyle = System.Windows.Forms.BorderStyle.None
		picFaciaEditor.Visible = True
		picFaciaEditor.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
		'UPGRADE_WARNING: Picture has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		picFaciaEditor.BackgroundImage = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Editor_S.jpg") 'Setup the correct picture size etc.
		
		'UPGRADE_WARNING: Control property picFaciaEditor.Parent was upgraded to picFaciaEditor.FindForm which has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="DFCDE711-9694-47D7-9C50-45A99CD8E91E"'
		SetWindowRgn(picFaciaEditor.FindForm.Handle.ToInt32, RegionEditor, True) 'Set the region of the Settings box outside which nothing will be displayed. The region has been
		
		Me.picRapidDisplay.Top = 98
		Me.imgSlowDisplay.Top = 98
		Me.picRapidDisplay.Left = 342
		Me.imgSlowDisplay.Left = 342
		
		Me.picRapidDisplay.Width = 572
		Me.imgSlowDisplay.Width = 572
		Me.picRapidDisplay.Height = 429
		Me.imgSlowDisplay.Height = 429
		
		Call GreenButton() 'Print the Build Rapid Button with Red Text
		
		Me.Show()
		
		
		
	End Sub
	
	Public Sub ActionEditor() 'This is called from the main program loop and contains any actions that need to be performed every program cycle.
		
		If ActiveWindowIs <> "EditorWindow" Then Exit Sub 'If the editor window is not active, dont take any action
		
		Me.txtFrameSelected.Text = "Name: Frame_" & VB6.GetItemString(lstXSheet, EditorListPosition) 'Show the frame name in the display window
		Me.txtListPosition.Text = "List Position: " & EditorListPosition + 1 'Output the actual position in the Xsheet in the text box
		
		
		Call EditorChecksWhenRapidPlayingAndLooping() 'Perform checks if the animation is playing or looping (moving slider etc.)
		Call AudioPlayer_Sync() 'Syncronise the audio soundtrack
		
		If RapidStatus = "Stopped" And SlowStatus = "Stopped" Then txtEditorPlayBackRate.Text = "Playback FPS: 0" 'If there is no playing set playback rate to zero
		If RapidStatus = "Playing" Or RapidStatus = "Looping" Then txtEditorPlayBackRate.Text = "Playback FPS: " & FPS 'If we are playing in rapidmode set text to full framerate
		
		
		'Check if the slider is being pressed. This slider is in a separate picturebox, otherwise you have to refresh the whole form to display the slider movement. This causes fickering
		'of the main display window. This doesn't occur on the buttons window as the displayed image is in a separate form, but it does here.
		
		If GetAsyncKeyState(VK_LBUTTON) Then 'Check the Left mouse is being pressed
			
			Call GetCursorPos(MousePoint) 'Get the screen position of the cursor
			Call ScreenToClient(Me.picEditorSlider.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of Buttons picture window
			
			If MousePoint.X > 0 And MousePoint.X < Me.picEditorSlider.Width Then
				If MousePoint.Y > 0 And MousePoint.Y < Me.picEditorSlider.Height Then
					'Stop the media player
					Call StopAllPlayback() 'Stop all playback
					Call PositionEditorSlider(2)
					
					EditorListPosition = (CInt(Int((MousePoint.X / Me.picEditorSlider.Width) * lstXSheet.Items.Count))) 'Calculate which frame should be displayed
					GetFrameImage() 'Calculate and display the frame
					
				End If
			End If
			
		End If
		
		
	End Sub
	Private Sub EditorChecksWhenRapidPlayingAndLooping() 'Perform checks if the animation is playing or looping (moving slider etc.)
		
		If RapidStatus = "Stopped" Then Exit Sub 'Don't take any action if the rapid player is stopped
		
		'Check if the rapid play has reached the end of the .avi and needs to stop
		If (EditorMediaPosition.CurrentPosition = EditorMediaPosition.Duration) And (RapidStatus = "Playing") Then 'rapid play has reached the end
			RapidStatus = "Stopped" 'Change the status to stopped
			Call AudioPlayer_Stop() 'Stop the audio track
			EditorListPosition = lstXSheet.Items.Count - 1 'Set list position to the total number of items in the xsheet -1 (sheet starts at 0)
			GetFrameImage() 'Get the frame and display it
			Call PositionEditorSlider(1) 'Position the slider control correctly
			
			picRapidDisplay.Visible = False 'Hide the rapid window
			
		End If
		
		'check if the rapid play has reached the end of the .avi and needs to loop around
		If (EditorMediaPosition.CurrentPosition = EditorMediaPosition.Duration) And (RapidStatus = "Looping") Then 'rapid play has reached the end and is in looping mode
			EditorMediaPosition.CurrentPosition = 0 'Take rapid play back to the start
		End If
		
		If (RapidStatus = "Playing") Or (RapidStatus = "Looping") Then 'If the rapid play is playing or looping adjust the slider bar
			EditorListPosition = (EditorMediaPosition.CurrentPosition * FPS)
			If EditorListPosition > lstXSheet.Items.Count - 1 Then EditorListPosition = lstXSheet.Items.Count - 1 'if its past the end (maybe a rounding error), set it to the last frame
			If EditorListPosition < 0 Then EditorListPosition = 0 'check its is not before start (rounding error) set it to the first frame
			Call PositionEditorSlider(1)
		End If
	End Sub
	Private Sub PositionEditorSlider(ByRef Mode As Short) 'Position Slider correctly
		' This command can be called in two modes
		'Mode 1 - It calculates the slider position from the position in the xsheet list (EditorListPosition)
		'Mode 2 - It calculates it from the position of the cursor over the slider
		
		If Mode = 1 Then 'Calculate the slider position relative to the EditorListPosition variable
			EditorSliderPosition = CInt((CDbl(EditorListPosition + 1) / CDbl(lstXSheet.Items.Count)) * Me.picEditorSlider.Width)
		End If
		
		If Mode = 2 Then 'Calculate the slider position using the cursor position
			EditorSliderPosition = MousePoint.X
		End If
		
		If EditorSliderPosition < 3 Then EditorSliderPosition = 3 'Limit the display of the slider so it doesnt go over the end
		If EditorSliderPosition > Me.picEditorSlider.Width - 3 Then EditorSliderPosition = Me.picEditorSlider.Width - 3 'Limit the display of the slider so it doesnt go over the end

        Dim sliderGraph As Graphics = Me.picEditorSlider.CreateGraphics
        Dim sliderhdc As IntPtr = sliderGraph.GetHdc
        Dim avgGraph As Graphics = frmAveraging.picSlider.CreateGraphics
        Dim avghdc As IntPtr = avgGraph.GetHdc

		'UPGRADE_ISSUE: Constant vbWhiteness was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picEditorSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(sliderhdc, 0, 0, Me.picEditorSlider.Width, Me.picEditorSlider.Height, 0, 0, 0, vbWhiteness) 'overwite the slider to erase the previous position
		'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		'UPGRADE_ISSUE: PictureBox property picEditorSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(sliderhdc, EditorSliderPosition - 3, 1, 6, 13, avghdc, 0, 0, vbSrcCopy) 'blit the slider to the screen

        sliderGraph.ReleaseHdc(sliderhdc)
        sliderGraph.Dispose()
        avgGraph.ReleaseHdc(avghdc)
        avgGraph.Dispose()

        Me.picEditorSlider.Refresh() 'Refresh the slider control to show the changes
	End Sub
	Private Sub GetFrameImage() 'This uses the EditorListPosition Variable to get the frame and display the image (This number starts at zero)
		'It extracts the text and determines which frame is required.
		Dim ReturnBoolean As Boolean
		If EditorListPosition < 0 Then EditorListPosition = 0 'Ensure we cannot go before the first item
		If EditorListPosition > lstXSheet.Items.Count - 1 Then EditorListPosition = lstXSheet.Items.Count - 1 'if you delete the last frame, list position needs reseting to the new last frame
		ReturnBoolean = ExtractFrameToBytes(CStr(VB6.GetItemString(lstXSheet, EditorListPosition))) 'Extracts the jpeg frame data from the avi file directly to RawJpegData() byte array
		imgSlowDisplay.Image = PictureFromByteStream(RawJpegData) 'show the image directly from the byte array to the image box
		
	End Sub
	
	Public Sub RedButton() 'Prints the Build Rapid Button with Red Text
		'frmEditor.picFaciaEditor.Font = "Impact"
		Me.picFaciaEditor.Font = VB6.FontChangeName(Me.picFaciaEditor.Font, "Comic Sans MS")
		Me.picFaciaEditor.Font = VB6.FontChangeSize(Me.picFaciaEditor.Font, 10)
		'frmEditor.picFaciaEditor.FontBold = False
		Me.picFaciaEditor.Font = VB6.FontChangeBold(Me.picFaciaEditor.Font, True)
		Me.picFaciaEditor.ForeColor = System.Drawing.ColorTranslator.FromOle(&HC0)
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.CurrentX was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'        
        'Me.picFaciaEditor.CurrentX = 633
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'Me.picFaciaEditor.CurrentY = 641
		'UPGRADE_ISSUE: PictureBox method picFaciaEditor.Print was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'Me.picFaciaEditor.Print("BUILD RAPID")

        Dim l As New Label
        l.Size = New Size(100, 50)
        l.Location = New Point(633, 641)
        l.Text = "BUILD RAPID"
        Me.picFaciaEditor.Controls.Add(l)
        


	End Sub
	
	Private Sub GreenButton() 'Prints the Build Rapid Button with Green Text
		'frmEditor.picFaciaEditor.Font = "Impact"
		Me.picFaciaEditor.Font = VB6.FontChangeName(Me.picFaciaEditor.Font, "Comic Sans MS")
		Me.picFaciaEditor.Font = VB6.FontChangeSize(Me.picFaciaEditor.Font, 10)
		'frmEditor.picFaciaEditor.FontBold = False
		Me.picFaciaEditor.Font = VB6.FontChangeBold(Me.picFaciaEditor.Font, True)
		Me.picFaciaEditor.ForeColor = System.Drawing.ColorTranslator.FromOle(&H8000)
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.CurrentX was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'Me.picFaciaEditor.CurrentX = 633
		'UPGRADE_ISSUE: PictureBox property picFaciaEditor.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'Me.picFaciaEditor.CurrentY = 641
		'UPGRADE_ISSUE: PictureBox method picFaciaEditor.Print was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'Me.picFaciaEditor.Print("BUILD RAPID")


        Dim g As Graphics = Graphics.FromImage(frmAveraging.picAverage.Image)
        Dim drawFont As New Font("Comic Sans MS", 10)
        Dim drawBrush As New SolidBrush(Color.Black)
        g.DrawString("BUILD RAPID", drawFont, drawBrush, 633, 641)

	End Sub
	Public Sub LoadAnimationToEditor() 'this is called each time the editor is opened (not just on first load up)
		ErrPnt(("5.00"))
		Dim X As Integer 'The Loop counter
		Dim FrameIDno As String
		Dim PhonemeText As String
		
		frmAnimator.Hide() 'hide the buttons form
		
		
		ErrorCode = 0
		NumLinesinXSheet = 46
		ReportReady = "Available"
		ReportNotReady = "Needed"
		RapidStatus = "Stopped"
		SlowStatus = "Stopped"
		ChangesMade = False 'The user has just entered the editor, so no changes have been made yet
		
		ReDim SourceFileNames(15) 'Redimension the aray to be able totake 15 names (0 to 14)
		SourceFileNames(0) = SavePath & ProjName & ".avi" 'Set the first filename to be the current avi file.
		NumSourceFiles = 1 'There is only one source file currently used
		SourceFile = SavePath & ProjName & ".avi"
		
		
		
		
		FileOpen(10, SourceFile, OpenMode.Binary, OpenAccess.Read) 'Open the avi file for reading
		If GetRefPoints = False Then
			FileClose(10)
			Call LeaveEditor()
		End If
		OriginalNumFrames = TotNumFrames 'Store the original number of frames as this may be required when leaving editor
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, FrameRate, 32 + 1)
		FrameRate = 1000000 / FrameRate 'Calculate the framerate in frames per second
		FileClose(10)
		
		'UPGRADE_WARNING: Timer property tmrPlay.Interval cannot have a value of 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="169ECF4A-1968-402D-B243-16603CC08604"'
		tmrPlay.Interval = 1000 / FrameRate 'set the playback timer to trigger at the framerate when it is called
		tmrEditorFrameRate.Interval = 1000
		lstXSheet.Items.Clear()
		lstClipboard.Items.Clear()
		
		For X = 1 To TotNumFrames ' Just load the ListBox up with the frame numbers
			
			FrameIDno = CStr(X)
			If Len(FrameIDno) = 1 Then FrameIDno = "A00000" & FrameIDno
			If Len(FrameIDno) = 2 Then FrameIDno = "A0000" & FrameIDno
			If Len(FrameIDno) = 3 Then FrameIDno = "A000" & FrameIDno
			If Len(FrameIDno) = 4 Then FrameIDno = "A00" & FrameIDno
			If Len(FrameIDno) = 5 Then FrameIDno = "A0" & FrameIDno
			
			lstXSheet.Items.Add(CStr(FrameIDno))
			
		Next 
		
		lstXNum.Items.Clear() 'Clear the left number list
		For X = 1 To NumLinesinXSheet 'Fill it with the initial position data
			FrameIDno = CStr(X)
			If Len(FrameIDno) = 1 Then FrameIDno = "00000" & FrameIDno
			If Len(FrameIDno) = 2 Then FrameIDno = "0000" & FrameIDno
			If Len(FrameIDno) = 3 Then FrameIDno = "000" & FrameIDno
			If Len(FrameIDno) = 4 Then FrameIDno = "00" & FrameIDno
			If Len(FrameIDno) = 5 Then FrameIDno = "0" & FrameIDno
			lstXNum.Items.Add(FrameIDno)
		Next X
		
		Call frmXSheet.FillExposureSheetArray() 'Fill the xsheet array phoneme() from the file
		
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(XSheetName) <> "" Then 'Check if phoneme file exists
			lstPhoneme.Items.Clear()
			For X = 1 To NumLinesinXSheet
				If X - 1 + XSheetOffset > UBound(Phoneme) Then GoTo EndofPhonemeList : 'There isnt sufficient data to fill all lines in the x sheet so leave early
				lstPhoneme.Items.Add(Phoneme(X - 1 + XSheetOffset)) 'add the item to the phoneme list. -1 because phoneme array starts at zero not 1
			Next X
EndofPhonemeList: 
		End If
		
		
		
		
		
		
		EditorListPosition = TotNumFrames - 1
		GetFrameImage() 'Get the last frame and display it
		txtRapidText.Text = ReportNotReady
		Call RedButton()
		
		'  Call BitBlt(frmEditor.picFaciaEditor.hdc, EditorSliderLeft, EditorSliderTop, EditorSliderWidth, EditorSliderHeight, 0, 0, 0, vbWhiteness)  'overwite the slider to erase the previous position
		'  Call BitBlt(frmEditor.picFaciaEditor.hdc, EditorSliderLeft + EditorSliderWidth - 6, EditorSliderTop + 1, 6, 13, frmAveraging.picSlider.hdc, 0, 0, vbSrcCopy) 'blit the slider to the screen
		'  frmEditor.picFaciaEditor.Refresh

        Dim sliderGraph As Graphics = Me.picEditorSlider.CreateGraphics
        Dim sliderHDC As IntPtr = sliderGraph.GetHdc
        Dim avgGraph As Graphics = frmAveraging.picSlider.CreateGraphics
        Dim avgHDC As IntPtr = avgGraph.GetHdc

		'UPGRADE_ISSUE: Constant vbWhiteness was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picEditorSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(sliderHDC, 0, 0, Me.picEditorSlider.Width, Me.picEditorSlider.Height, 0, 0, 0, vbWhiteness) 'overwite the slider to erase the previous position
		'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		'UPGRADE_ISSUE: PictureBox property picEditorSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(sliderHDC, Me.picEditorSlider.Width - 6, 1, 6, 13, avgHDC, 0, 0, vbSrcCopy) 'blit the slider to the screen
        Me.picEditorSlider.Refresh()

        sliderGraph.ReleaseHdc(sliderHDC)
        sliderGraph.Dispose()
        avgGraph.ReleaseHdc(avgHDC)
        avgGraph.Dispose()
		
		
		
	End Sub
	
	
	
	Private Sub picFaciaEditor_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles picFaciaEditor.MouseUp
		Dim Button As Short = eventArgs.Button \ &H100000
		Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
		Dim X As Single = eventArgs.X
		Dim Y As Single = eventArgs.Y 'Mouse button has been released over the settings form
		Dim ReturnError As Boolean
		If ActiveWindowIs <> "EditorWindow" Then Exit Sub 'If the editor window is not active, dont action any buttons
		
		If Button = VB6.MouseButtonConstants.LeftButton Then 'The mouse button is the left button
			Red = 0 : Green = 0 : Blue = 0 'reset the rgb values
			Call GetCursorPos(MousePoint) 'Get the screen position of the cursor
			Call ScreenToClient(Me.picFaciaEditor.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of editor window
			PixCol = GetPixel(Hotspot_Editor, MousePoint.X, MousePoint.Y) 'Get the relative colour at the mouse point of the hotspot image in memory
			Red = PixCol And &HFF : Green = CShort(PixCol And &HFF00) / 256 : Blue = CShort(PixCol And &HFF0000) / 65535 'Get the value of each color byte (rgb)
			
			
			If Red = 0 And Green = 100 And Blue = 0 Then Call EditorToStart() 'To Start Button has been released
			If Red = 100 And Green = 0 And Blue = 0 Then Call EditorBack1() 'Back 1 Button has been released
			If Red = 0 And Green = 50 And Blue = 0 Then Call EditorStopButt() 'Stop Button has been released
			If Red = 50 And Green = 0 And Blue = 0 Then Call EditorPlayButt() 'Play Button has been released
			If Red = 0 And Green = 0 And Blue = 50 Then Call EditorForward1() 'Forward 1 Button has been released
			If Red = 0 And Green = 0 And Blue = 100 Then Call EditorToEnd() 'To end Button has been released
			If Red = 100 And Green = 100 And Blue = 0 Then Call EditorLoopButt() 'Loop Button has been released
			If Red = 98 And Green = 10 And Blue = 108 Then Call EditorPasteAfter() 'Paste After Button has been released
			If Red = 179 And Green = 5 And Blue = 177 Then Call EditorCutToClip() 'Cut to clipboard has been released
			If Red = 253 And Green = 57 And Blue = 251 Then Call EditorCopyToClip() 'Copy to clipboard Button has been released
			If Red = 10 And Green = 108 And Blue = 105 Then Call EditorDelete() 'Delete Button has been released
			If Red = 2 And Green = 119 And Blue = 218 Then Call EditorDuplicate1Second() 'Duplicate 1 second Button has been released
			If Red = 146 And Green = 155 And Blue = 246 Then Call EditorPasteAfterInverted() 'Paste After Inverted Button has been released
			If Red = 1 And Green = 255 And Blue = 37 Then ReturnError = EditorRapidBuild 'Rapid Build Button has been released
			If Red = 246 And Green = 147 And Blue = 134 Then Call SoundToggle() 'Sound Toggle Button has been released
			If Red = 79 And Green = 14 And Blue = 11 Then Call XSheetToggle() 'X Sheet Toggle Button has been released
			If Red = 206 And Green = 185 And Blue = 245 Then Call ImportToEditor() 'Import Button has been released released
			
			
			If Red = 85 And Green = 129 And Blue = 9 Then 'You have released the quit button on the Editor form
				Call EditorStopButt() 'Stop any animation if its playing
				ErrorCodeNo = "00.00" 'Tell the program it is a normal exit
				Call ExitProgram() 'Call the exit routine
			End If
			If Red = 244 And Green = 132 And Blue = 0 Then Call LeaveEditor() 'You have released the Apply Button on the Editor form
		End If
	End Sub
	Private Sub ImportToEditor() 'Imports frames from another avi file to the editor.
		ErrPnt(("5.01"))
		Dim NewSourceFileName As String
		Dim ResolutionCheckX1 As Integer
		Dim ResolutionCheckY1 As Integer
		Dim NumberOfFramesToadd As Integer
		Dim FrameIDno1 As String
		Dim X As Integer
		
		On Error GoTo HandleError
		
		If NumSourceFiles >= 15 Then
			Call MsgBox("I cannot currently work with more than 15 source files." & Chr(13) & Chr(10) & "You can overcome this limitation, by applying the changes you" & Chr(13) & Chr(10) & "have made, leaving the editor and then returning to add more.", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		CommonEditorDialog1Open.Filter = "Select Existing File (*.avi) | *.avi" 'Set the common dialog box to only show .avi files
		CommonEditorDialog1Open.InitialDirectory = SavePath 'Set the directory to look in as the current directory
		CommonEditorDialog1Open.ShowDialog() 'Display the file dialog to the user
		NewSourceFileName = CommonEditorDialog1Open.FileName
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(CommonEditorDialog1Open.FileName) = "" Then Exit Sub 'The file doesn' exist, so exit the subroutine
		
		
		'Check the resolution of the image matches the current camera settings
		FileOpen(18, CommonEditorDialog1Open.FileName, OpenMode.Binary, OpenAccess.Read)
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(18, NumberOfFramesToadd, 48 + 1) 'Get the total number of frames to add to the list
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(18, ResolutionCheckX1, 184 + 1) 'Get the resolution of the existing file
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(18, ResolutionCheckY1, 188 + 1) 'Get the resolution of the existing file
		FileClose(18)
		
		If (ResolutionCheckX1 <> CaptureWidth) Or (ResolutionCheckY1 <> CaptureHeight) Then
			Call MsgBox("I cannot import this file because the file image resolution is" & Chr(13) & Chr(10) & ResolutionCheckX1 & " X " & ResolutionCheckY1 & " and your current working file is " & CaptureWidth & " X " & CaptureHeight & Chr(13) & Chr(10) & "Pick a file that is of a similar resolution", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub 'the newly selected file is at a different resolution to the source device so dont load it
		End If
		
		'We now have a valid file so begin to add it to the frame list
		
		NumSourceFiles = NumSourceFiles + 1 'Increase the source file count.
		SourceFileNames(NumSourceFiles - 1) = CommonEditorDialog1Open.FileName 'store the new filename in the array
		
		For X = 1 To NumberOfFramesToadd ' Just load the ListBox up with the frame numbers
			
			FrameIDno1 = CStr(X)
			If Len(FrameIDno1) = 1 Then FrameIDno1 = "00000" & FrameIDno1
			If Len(FrameIDno1) = 2 Then FrameIDno1 = "0000" & FrameIDno1
			If Len(FrameIDno1) = 3 Then FrameIDno1 = "000" & FrameIDno1
			If Len(FrameIDno1) = 4 Then FrameIDno1 = "00" & FrameIDno1
			If Len(FrameIDno1) = 5 Then FrameIDno1 = "0" & FrameIDno1
			
			FrameIDno1 = Chr(CShort(NumSourceFiles + 64)) & FrameIDno1 'Converts the numsourcefiles to a letter and adds it to the start ie 2 = B, 3 = C etc.
			
			lstXSheet.Items.Add(CStr(FrameIDno1)) 'Adds the new frame IDs to the end of the list
			
		Next X
		
		ChangesMade = True 'Log that there has been changes to the X sheet
		
		On Error GoTo 0
		ErrPnt(("5.02"))
		Exit Sub
		'*******************************************************************************************************
HandleError: 
		'an error is generated if the user hits the cancel button when selecting a file, so justexit the routine
		Exit Sub
		
	End Sub
	Private Sub LeaveEditor()
		Dim W As Integer 'Loop counter
		Dim FileNameToMove As String
		Dim NewFileName As String
		Dim ReturnError As Boolean
		Call EditorStopButt() 'stop any animation if its playing
		Call RemoveDShowEditor() 'clean up Editor media player and variables if they are active
		If ChangesMade = False Then GoTo LeaveUnderErrorCondition 'There has been no changes made, so just exit without save
		
		If ErrorCode <> 0 Then
			Call MsgBox("Editor Error Code = " & ErrorCode & " - This file appears corrupt, I cannot edit it", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			GoTo LeaveUnderErrorCondition
		End If
		Erase RawJpegData
		Dim SourceId As Integer
		Dim FrameIDNumber As Integer
		Dim SourceFileName As String
		Dim FrameIDText1 As String 'The frame number extracted from the string 'The first character of the string, ie A,B,C,D transposed to A = 0 B = 1 C = 2 etc
		If MsgBox("Do you want to apply the changes you have made?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Helium Frog Animator") = MsgBoxResult.Yes Then
			If txtRapidText.Text = ReportNotReady Then
				If EditorRapidBuild = False Then 'Rebuild the rapid file, if it fails to build, dont exit the editor,
					Exit Sub
				End If
			End If
			
			RemoveDShowPlayBack() 'Stop the current video playback
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(SavePath & ProjName & ".avi") <> "" Then Kill((SavePath & ProjName & ".avi")) 'Erase the currently active file
			Rename(My.Application.Info.DirectoryPath & "\RapidBuild.avi", SavePath & ProjName & ".avi") 'Rename the edited file to replace the old one
			
			FileOpen(18, SavePath & ProjName & ".avi", OpenMode.Binary, OpenAccess.Read)
			'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FileGet(18, FrameNum, 48 + 1) 'Get the total number of frames as specified in the header
			FileClose(18)
			
			FunctionOK = OpenDShowPlayBack(SavePath & ProjName & ".avi", (frmCapture.picPlayBack)) 'Load the new video in the playback window
			If ExtractLastFrame() = True Then 'Extract the last frame available for onion skin
				frmAveraging.picOnion1.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Undo.jpg") 'Load the picture into the onion buffer
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(My.Application.Info.DirectoryPath & "\Undo.jpg") <> "" Then Kill((My.Application.Info.DirectoryPath & "\Undo.jpg")) 'Erase the temporary Undo frame
			Else
				Call MsgBox("I failed to extract the last frame for the onion skin routine", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			End If
			
			txtRapidText.Text = "Saving Files"
			txtRapidText.Refresh()
			'Now search for individual bitmap ang jpeg, pngs etc and rename them with a unique extension
			For W = 1 To FrameNum
				NewFileName = LTrim(Str(W))
				If Len(NewFileName) = 1 Then NewFileName = "00000" & NewFileName
				If Len(NewFileName) = 2 Then NewFileName = "0000" & NewFileName
				If Len(NewFileName) = 3 Then NewFileName = "000" & NewFileName
				If Len(NewFileName) = 4 Then NewFileName = "00" & NewFileName
				If Len(NewFileName) = 5 Then NewFileName = "0" & NewFileName
				NewFileName = SavePath & ProjName & "_Frame_" & NewFileName
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".bmp") <> "" Then Rename(NewFileName & ".bmp", NewFileName & ".bmp" & "#_@") 'Rename the file with a new extension
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".gif") <> "" Then Rename(NewFileName & ".gif", NewFileName & ".gif" & "#_@") 'Rename the file with a new extension
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".jpg") <> "" Then Rename(NewFileName & ".jpg", NewFileName & ".jpg" & "#_@") 'Rename the file with a new extension
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".png") <> "" Then Rename(NewFileName & ".png", NewFileName & ".png" & "#_@") 'Rename the file with a new extension
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".tif") <> "" Then Rename(NewFileName & ".tif", NewFileName & ".tif" & "#_@") 'Rename the file with a new extension
			Next W
			
			
			'Now go through the X sheet list finding individual frames copying them to the new names
			SourceId = -999
			
			For W = 0 To lstXSheet.Items.Count - 1
				NewFileName = LTrim(Str(W + 1))
				If Len(NewFileName) = 1 Then NewFileName = "00000" & NewFileName
				If Len(NewFileName) = 2 Then NewFileName = "0000" & NewFileName
				If Len(NewFileName) = 3 Then NewFileName = "000" & NewFileName
				If Len(NewFileName) = 4 Then NewFileName = "00" & NewFileName
				If Len(NewFileName) = 5 Then NewFileName = "0" & NewFileName
				NewFileName = SavePath & ProjName & "_Frame_" & NewFileName
				FrameIDText1 = CStr(VB6.GetItemString(lstXSheet, W))
				SourceId = CInt(Asc(VB.Left(FrameIDText1, 1)) - 65) 'Sets the variable to the first character. A = 0 B = 1 C = 2 etc
				SourceFileName = SourceFileNames(SourceId) 'Extract the source file name from the array
				SourceFileName = StrReverse(SourceFileName)
				SourceFileName = Mid(SourceFileName, 5)
				SourceFileName = StrReverse(SourceFileName)
				'add the Frame number of the source file here
				SourceFileName = SourceFileName & "_Frame_" & Mid(FrameIDText1, 2)
				FrameIDNumber = CInt(Val(Mid(CStr(VB6.GetItemString(lstXSheet, W)), 2))) 'Extracts the frame id from the string
				
				If SourceId = 0 Then 'If the source file comes from the original file it has been renamed , copy the files across.
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".bmp" & "#_@") <> "" Then FileCopy(SourceFileName & ".bmp" & "#_@", NewFileName & ".bmp")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".gif" & "#_@") <> "" Then FileCopy(SourceFileName & ".gif" & "#_@", NewFileName & ".gif")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".jpg" & "#_@") <> "" Then FileCopy(SourceFileName & ".jpg" & "#_@", NewFileName & ".jpg")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".png" & "#_@") <> "" Then FileCopy(SourceFileName & ".png" & "#_@", NewFileName & ".png")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".tif" & "#_@") <> "" Then FileCopy(SourceFileName & ".tif" & "#_@", NewFileName & ".tif")
				Else
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".bmp") <> "" Then FileCopy(SourceFileName & ".bmp", NewFileName & ".bmp")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".gif") <> "" Then FileCopy(SourceFileName & ".gif", NewFileName & ".gif")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".jpg") <> "" Then FileCopy(SourceFileName & ".jpg", NewFileName & ".jpg")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".png") <> "" Then FileCopy(SourceFileName & ".png", NewFileName & ".png")
					'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
					If Dir(SourceFileName & ".tif") <> "" Then FileCopy(SourceFileName & ".tif", NewFileName & ".tif")
				End If
			Next W
			
			'Now search for the originally renamed files and delete them
			For W = 1 To FrameNum
				NewFileName = LTrim(Str(W))
				If Len(NewFileName) = 1 Then NewFileName = "00000" & NewFileName
				If Len(NewFileName) = 2 Then NewFileName = "0000" & NewFileName
				If Len(NewFileName) = 3 Then NewFileName = "000" & NewFileName
				If Len(NewFileName) = 4 Then NewFileName = "00" & NewFileName
				If Len(NewFileName) = 5 Then NewFileName = "0" & NewFileName
				NewFileName = SavePath & ProjName & "_Frame_" & NewFileName
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".bmp" & "#_@") <> "" Then Kill((NewFileName & ".bmp" & "#_@"))
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".gif" & "#_@") <> "" Then Kill((NewFileName & ".gif" & "#_@"))
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".jpg" & "#_@") <> "" Then Kill((NewFileName & ".jpg" & "#_@"))
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".png" & "#_@") <> "" Then Kill((NewFileName & ".png" & "#_@"))
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(NewFileName & ".tif" & "#_@") <> "" Then Kill((NewFileName & ".tif" & "#_@"))
			Next W
			
		End If
		
		
		
LeaveUnderErrorCondition: 
		ActiveWindowIs = "ButtonWindow" 'Make Buttons window active again so buttons and hotkeys will now work again
		
		frmXSheet.ReloadExposureSheet((FrameNum))
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\RapidBuild.avi") <> "" Then Kill((My.Application.Info.DirectoryPath & "\RapidBuild.avi")) 'Erase the RapidBuild.avi file it if is still there
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\TempIndex.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\TempIndex.txt")) 'Erase the temp index file if it is still there
		Me.Visible = False 'Hide the Editor form
		frmAnimator.Show() 'show the buttons form
		ErrPnt(("5.99"))
	End Sub
	'UPGRADE_ISSUE: ListBox event lstXSheet.Scroll was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="ABD9AF39-7E24-4AFF-AD8D-3675C1AA3054"'
	Private Sub lstXSheet_Scroll() 'The user is scrolling the X sheet
		Dim Y As Integer
		Dim FrameIDno As String
		
		lstXNum.Items.Clear() 'Clear the left number list
		For Y = lstXSheet.TopIndex To lstXSheet.TopIndex + NumLinesinXSheet - 1 'get the top index of the x sheet and reprint the numbers in the number list
			FrameIDno = CStr(Y + 1)
			If Len(FrameIDno) = 1 Then FrameIDno = "00000" & FrameIDno
			If Len(FrameIDno) = 2 Then FrameIDno = "0000" & FrameIDno
			If Len(FrameIDno) = 3 Then FrameIDno = "000" & FrameIDno
			If Len(FrameIDno) = 4 Then FrameIDno = "00" & FrameIDno
			If Len(FrameIDno) = 5 Then FrameIDno = "0" & FrameIDno
			lstXNum.Items.Add(FrameIDno)
		Next Y
		
		lstPhoneme.Items.Clear()
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(XSheetName) <> "" Then 'Check if phoneme file exists
			For Y = lstXSheet.TopIndex + XSheetOffset To lstXSheet.TopIndex + NumLinesinXSheet - 1 + XSheetOffset
				If Y > UBound(Phoneme) Then GoTo EndofPhonemeList1 : 'There isnt sufficient data to fill all lines in the x sheet so leave early
				lstPhoneme.Items.Add(Phoneme(Y)) 'add the item to the phoneme list. -1 because phoneme array starts at zero not 1
			Next Y
		End If
EndofPhonemeList1: 
	End Sub
	Private Function EditorRapidBuild() As Boolean 'This builds the animation from the xsheet
		ErrPnt(("5.03"))
		EditorRapidBuild = False 'set the function to initially return an error
		
		Dim Y As Integer
		Dim ByteValue As Byte 'A variable to hold a single byte
		Dim OffsetPoint As Integer 'The offset of each frame from the start of the file
		Dim TotalFrameCount As Integer 'The total number of frames to save. (dont use lstXsheet.ListCount) as it is an integer and overflows if there are a large number of frames
		Dim FileLengthCheck As Double 'This is used to check if the .avi file length of 2GB is going to be exceeded.
		Dim FramesAdded As Integer 'The total number of frames added to the file. Keep a running tally in case we have to quit early due to file size limit
		'Dim TotalJpegDataLength As Long 'The total length of the jpeg data
		
		Call EditorStopButt() 'Trigger the stop button to stop any playback etc.
		
		TotalFrameCount = CInt(lstXSheet.Items.Count)
		If txtRapidText.Text = ReportReady Then 'The file is already built, so exit the subroutine
			Beep()
			Exit Function
		End If
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\RapidBuild.avi") <> "" Then Kill((My.Application.Info.DirectoryPath & "\RapidBuild.avi")) 'Erase any previous file
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\TempIndex.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\TempIndex.txt")) 'Erase any previous file
		
		FileOpen(15, My.Application.Info.DirectoryPath & "\RapidBuild.avi", OpenMode.Binary) 'Open The destination file.
		FileOpen(10, SourceFile, OpenMode.Binary, OpenAccess.Read) 'Open the avi file for reading
		FileOpen(16, My.Application.Info.DirectoryPath & "\TempIndex.txt", OpenMode.Binary) 'Open The temporary index'
		Call GetRefPoints()
		For Y = 0 To MoviStart
			'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FileGet(10, ReadinData1, Y + 1) 'Extract data from that line
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(15, ReadinData1, Y + 1) 'Put it in the destination file
		Next Y
		
		'write the start of the index file
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(16, &H31786469) 'put in the "idx1" header
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(16, CInt(TotalFrameCount * 16)) 'The length of the index file (number of frames in xsheet * 16)
		
		'Now get the jpeg data across to new file
		For Y = 0 To lstXSheet.Items.Count - 1
			ReturnCheck = ExtractFrameToBytes(CStr(VB6.GetItemString(lstXSheet, Y))) 'Extracts the jpeg frame data from the avi file directly to RawJpegData() byte array
			
			OffsetPoint = LOF(15) 'get the point at which the data is written to (for index)
			FileLengthCheck = CDbl(LOF(15)) + CDbl(UBound(RawJpegData) + 1) + CDbl((Y + 1) * 16) + CDbl(100) 'Calcuate the expected length of the file (currentlength + new data + index) + 100 bytes for good measure!
			If FileLengthCheck > CDbl(CDbl(2) * CDbl(1024) * CDbl(1024) * CDbl(1024)) Then
				Call MsgBox("Adding frame " & Y + 1 & " takes us beyond the .avi file limit of 2GB - Please reduce the number of frames", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
				GoTo OversizeFile
			End If
			
			
			'Build the main avi file data
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(15, &H62643030) '00db line
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(15, CInt(UBound(RawJpegData) + 1)) 'write the length of the data that follows
			'  TotalJpegDataLength = TotalJpegDataLength + CLng(UBound(RawJpegData) + 1) + 8      'Keep a running tally of how long the jpeg data is (+8 to include the 00db header)
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(15, RawJpegData) 'Write the raw jpeg data
			
			'Now build the index file part
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(16, &H62643030) 'write the 00db marker to the file
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(16, CInt(16)) 'write the keyframes (always is set to 16 - all are keyframes)
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(16, CInt(OffsetPoint)) 'write the offset from the start of the file where data is
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(16, CInt(UBound(RawJpegData) + 1)) 'write the length of the jpeg data data that follows
			txtRapidText.Text = "Building - " & Int((Y / TotalFrameCount) * 100) & " %"
			txtRapidText.Refresh()
			FramesAdded = Y + 1
		Next Y
		
		txtRapidText.Text = "Building - 100 %"
		txtRapidText.Refresh()
		'check here whether the file length is exactly divisible by 2 (Format states it must be rounded to a WORD boundary ie 2 bytes)
		If LOF(15) Mod 2 <> 0 Then 'Divide the length by 2 and find the remainder
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(15, CByte(1)) 'The data length is not divisable exactly by 2 so add a padding byte (has value of 01 for some reason)
		End If
		
		
		'now transfer the index data in
		For Y = 1 To LOF(16) 'write the new index data from the IndexStore file to the end of the Motion Jpeg file
			'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FileGet(16, ByteValue, Y) 'Get the value from the index file
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(15, ByteValue) 'Write it to the end of the file
		Next Y
		
		FileClose(16)
		FileClose(10)
		
		
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(15, CInt(LOF(15) - 8), 4 + 1) 'Set the total length of the file in header. This is total file length minus the first 8 bytes
		'   Put #15, 48 + 1, CLng(lstXSheet.ListCount)            'Set the total number of frames in header (Not actually required for file to work)
		'   Put #15, 140 + 1, CLng(lstXSheet.ListCount)           'Set the total number of frames in the AVIStreamheader structure (This is required)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(15, CInt(FramesAdded), 48 + 1) 'Set the total number of frames in header (Not actually required for file to work)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(15, CInt(FramesAdded), 140 + 1) 'Set the total number of frames in the AVIStreamheader structure (This is required)
		idx1Start = CInt(LOF(15) - (FramesAdded * 16) - 8) 'Calculate where "idx1" line should be (file length - index length)
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(15, ReadinData1, idx1Start + 1) 'Extract data from that line
		If ReadinData1 = &H31786469 Then 'Check if data contains "idx1"
		Else
			Call MsgBox("I cannot find the new idx1 header but found hex value " & "&H" & Hex(ReadinData1), MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		End If
		
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(15, CInt(idx1Start - 2556), 2552 + 1) 'Set the total length of the jpeg data, this is the index start minus the header which is always 2556 in my files
		
		
		
		
		FileClose(15)
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\TempIndex.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\TempIndex.txt")) 'Erase the index file
		
		Beep()
		
		txtRapidText.Text = ReportReady
		Call GreenButton()
		EditorRapidBuild = True
		ErrPnt(("5.04"))
		Exit Function
		
		'********************************************************
OversizeFile: 'The file is oversize, so exit and tidy up
		FileClose(15) 'Close the destination file
		FileClose(16) 'Close the temporary index file
		FileClose(10) 'Close the source file
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\RapidBuild.avi") <> "" Then Kill((My.Application.Info.DirectoryPath & "\RapidBuild.avi")) 'Erase the RapidBuild.avi file it if is still there
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\TempIndex.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\TempIndex.txt")) 'Erase the temp index file if it is still there
		EditorRapidBuild = False 'Make sure we are returning an error
	End Function
	Private Sub EditorToStart()
		Call StopAllPlayback() 'Stop any playback that is happening
		
		EditorListPosition = 0 'Set list position to zero
		GetFrameImage() 'Get the frame and display it
		Call PositionEditorSlider(1)
		Call AudioPlayer_Sync() 'Syncronise the audio track
	End Sub
	Private Sub EditorBack1()
		Call StopAllPlayback() 'Stop any playback that is happening
		
		EditorListPosition = EditorListPosition - 1 'Increment the list position
		If EditorListPosition < 0 Then EditorListPosition = 0 'check itsn not before the start
		GetFrameImage() 'Get the frame and display it
		Call PositionEditorSlider(1)
		Call AudioPlayer_Sync() 'Syncronise the audio track
	End Sub
	Private Sub EditorStopButt()
		
		If RapidStatus = "Playing" Then 'If a rapid build playing pause it and find the position it stopped at
			EditorMediaControl.Pause()
			EditorListPosition = (EditorMediaPosition.CurrentPosition * FPS) 'set the EditorListPosition variable to log wherw the rapid play stopped
			If EditorListPosition > lstXSheet.Items.Count - 1 Then EditorListPosition = lstXSheet.Items.Count - 1 'if its past the end (maybe a rounding error), set it to the last frame
			If EditorListPosition < 0 Then EditorListPosition = 0 'check its is not before start (rounding error) set it to the first frame
		End If
		Call StopAllPlayback() 'Stop any playback that is happening
		GetFrameImage() 'Get the frame and display it
		Call PositionEditorSlider(1)
		Call AudioPlayer_Stop() 'Stop the audio track
	End Sub
	Private Sub EditorPlayButt()
		
		If txtRapidText.Text = ReportReady Then 'If a rapid play is ready begin playing
			'UPGRADE_WARNING: Couldn't resolve default property of object OpenDShowEditor(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			FunctionOK = OpenDShowEditor(My.Application.Info.DirectoryPath & "\RapidBuild.avi", picRapidDisplay) 'Open the Video player
			'find out where the slow play window is, flip to rapid window and begin playing
			EditorMediaPosition.CurrentPosition = (EditorListPosition + 1) / FPS
			EditorMediaControl.Pause()
			RapidStatus = "Playing" 'Change the rapid window status to playing
			PauseDelay123((200)) 'This seem to stop a glitch whereby the rapid window is displayed before it is set to the correct frame
			picRapidDisplay.Visible = True 'Display the rapid window
			EditorMediaControl.Run()
			
		Else
			SlowStatus = "Playing"
			tmrPlay.Enabled = True 'Else begin playing the slow window
			tmrEditorFrameRate.Enabled = True
		End If
		Call AudioPlayer_Play() 'Begin playing the soundtrack
	End Sub
	Private Sub EditorLoopButt()
		
		If txtRapidText.Text = ReportReady Then 'If a rapid play is ready begin playing
			'UPGRADE_WARNING: Couldn't resolve default property of object OpenDShowEditor(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			FunctionOK = OpenDShowEditor(My.Application.Info.DirectoryPath & "\RapidBuild.avi", picRapidDisplay) 'Open the Video player
			'find out where the slow play window is, flip to rapid window and begin playing
			EditorMediaPosition.CurrentPosition = (EditorListPosition + 1) / FPS
			EditorMediaControl.Run()
			RapidStatus = "Looping" 'Change the rapid window status to playing
			picRapidDisplay.Visible = True 'Display the rapid window
		Else
			SlowStatus = "Looping"
			tmrPlay.Enabled = True 'Else begin playing the slow window
			tmrEditorFrameRate.Enabled = True
		End If
		Call AudioPlayer_Play() 'Begin playing the soundtrack
	End Sub
	Private Sub EditorForward1()
		Call StopAllPlayback() 'Stop any playback that is happening
		
		EditorListPosition = EditorListPosition + 1 'Increment the list position
		If EditorListPosition > lstXSheet.Items.Count - 1 Then EditorListPosition = lstXSheet.Items.Count - 1 'Check its not past the end
		GetFrameImage() 'Get the frame and display it
		Call PositionEditorSlider(1)
		Call AudioPlayer_Sync() 'Syncronise the audio track
	End Sub
	Private Sub EditorToEnd()
		Call StopAllPlayback() 'Stop any playback that is happening
		
		EditorListPosition = lstXSheet.Items.Count - 1 'Set list position to the total number of items in the xsheet -1 (sheet starts at 0)
		GetFrameImage() 'Get the frame and display it
		Call PositionEditorSlider(1)
		Call AudioPlayer_Sync() 'Syncronise the audio track
	End Sub
	Private Sub StopAllPlayback() 'This command stops all playback whether fast or slow
		RapidStatus = "Stopped" 'Make sure the status of the rapid window is stopped
		SlowStatus = "Stopped" 'Make sure the status of the slow window is stopped
		picRapidDisplay.Visible = False 'Make sure the rapid display window is hidden
		tmrPlay.Enabled = False 'Switch off playback if running
		tmrEditorFrameRate.Enabled = False
		Call RemoveDShowEditor() 'Clear the rapid playback media player if it exists
		Call AudioPlayer_Stop() 'Stop the audio track
		txtEditorPlayBackRate.Text = "Playback FPS: 0" 'Set the playback rate to zero
	End Sub
	
	
	Private Sub lstXSheet_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles lstXSheet.MouseUp
		Dim Button As Short = eventArgs.Button \ &H100000
		Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
		Dim X As Single = VB6.PixelsToTwipsX(eventArgs.X)
		Dim Y As Single = VB6.PixelsToTwipsY(eventArgs.Y)
		Dim Q As Integer
		Call StopAllPlayback() 'Stop any playback that is happening
		For Q = lstXSheet.Items.Count - 1 To 0 Step -1 'Go through all items from bottom to top, this finds the last item.
			If lstXSheet.GetSelected(Q) Then 'Check if the item is selected
				EditorListPosition = Q 'Set variable that holds the current frame
				GetFrameImage() 'Get the frame and display it
				Call PositionEditorSlider(1) 'Reposition the slider correctly
				txtListPosition.Text = "List Position: " & EditorListPosition + 1 'Change the display title bar
				Exit Sub 'Exit the subroutine as we have found the last selected frame
			End If
		Next Q
		
	End Sub
	
	Private Sub EditorDelete()
		Dim X As Integer
		FirstFound = -999 'Set the first found to an invalid value
		'First check to see if the X sheet will be left without any frames, this is not allowed. Otherwise you wouldn't be able to select a target point
		If lstXSheet.SelectedItems.Count >= lstXSheet.Items.Count Then
			Call MsgBox("An X sheet must contain at least one frame", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		txtRapidText.Text = ReportNotReady 'Inform program that a rapid play is not available
		Call StopAllPlayback() 'Stop any playback that is happening
		Call RedButton() 'Show red text on rapid playback button
		
		For X = lstXSheet.Items.Count - 1 To 0 Step -1 'Go through all items from bottom to top, this avoids items changing position whilst looping.
			If lstXSheet.GetSelected(X) Then 'Check if the item is selected
				If FirstFound = -999 Then FirstFound = X 'Only if this is the first selected log the position
				lstXSheet.Items.RemoveAt(X) 'Remove the item at that point
			End If
		Next X
		
		If FirstFound <> -999 Then 'There has been something removed
			EditorListPosition = FirstFound - 1 'Find the frame before the first deleted
			GetFrameImage() 'Display that image in the window
		End If
		
		ChangesMade = True 'Log that there has been changes to the X sheet
	End Sub
	
	Private Sub EditorCopyToClip()
		Dim X As Integer
		
		'First check to see if any items have been selected
		If lstXSheet.SelectedItems.Count < 1 Then
			Call MsgBox("No items selected in X sheet ", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		txtRapidText.Text = ReportNotReady 'Inform program that a rapid play is not available
		Call StopAllPlayback() 'Stop any playback that is happening
		Call RedButton() 'Show red text on rapid playback button
		
		lstClipboard.Items.Clear() 'Clear the clipboard
		For X = lstXSheet.Items.Count - 1 To 0 Step -1 'Go through all items from bottom to top, this avoids items changing position whilst looping.
			If lstXSheet.GetSelected(X) Then 'Check if the item is selected
				lstClipboard.Items.Insert(0, VB6.GetItemString(lstXSheet, X)) 'Add the item to the clipboard. Add at each top position so items are not reversed but shuffle down each time
			End If
		Next X
		
		ChangesMade = True 'Log that there has been changes to the X sheet
	End Sub
	Private Sub EditorCutToClip()
		Dim X As Integer
		FirstFound = -999 'Set the first found to an invalid value
		'First check to see if the X sheet will be left without any frames, this is not allowed. Otherwise you wouldn't be able to select a target point
		If lstXSheet.SelectedItems.Count >= lstXSheet.Items.Count Then
			Call MsgBox("An X sheet must contain at least one frame", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		txtRapidText.Text = ReportNotReady 'Inform program that a rapid play is not available
		Call StopAllPlayback() 'Stop any playback that is happening
		Call RedButton() 'Show red text on rapid playback button
		
		lstClipboard.Items.Clear() 'Clear the clipboard
		For X = lstXSheet.Items.Count - 1 To 0 Step -1 'Go through all items from bottom to top, this avoids items changing position whilst looping.
			If lstXSheet.GetSelected(X) Then 'Check if the item is selected
				lstClipboard.Items.Insert(0, VB6.GetItemString(lstXSheet, X)) 'Add the item to the clipboard. Add at each top position so items are not reversed but shuffle down each time
				lstXSheet.Items.RemoveAt(X) 'Remove the item at that point
				FirstFound = X 'This is a bottom to top removal, so log everything, then you are left with the top position of the removed item
			End If
		Next X
		
		If FirstFound <> -999 Then 'There has been something removed
			EditorListPosition = FirstFound - 1 'Find the frame before the topmost removed frame
			GetFrameImage() 'Display that image in the window
		End If
		
		ChangesMade = True 'Log that there has been changes to the X sheet
	End Sub
	Private Sub EditorPasteAfter()
		Dim X As Integer
		
		'First check if only one frame has been selected in the X sheet for the target point
		If lstXSheet.SelectedItems.Count <> 1 Then
			Call MsgBox("Select only 1 frame for target point", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		txtRapidText.Text = ReportNotReady 'Inform program that a rapid play is not available
		Call StopAllPlayback() 'Stop any playback that is happening
		Call RedButton() 'Show red text on rapid playback button
		
		For X = lstClipboard.Items.Count - 1 To 0 Step -1 'Go through all items from bottom to top, this ensures they are pasted back in the correct order.
			lstXSheet.Items.Insert(lstXSheet.SelectedIndex + 1, VB6.GetItemString(lstClipboard, X))
		Next X
		
		ChangesMade = True 'Log that there has been changes to the X sheet
	End Sub
	
	Private Sub EditorPasteAfterInverted()
		Dim X As Integer
		
		'First check if only one frame has been selected in the X sheet for the target point
		If lstXSheet.SelectedItems.Count <> 1 Then
			Call MsgBox("Select only 1 frame for target point", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		txtRapidText.Text = ReportNotReady 'Inform program that a rapid play is not available
		Call StopAllPlayback() 'Stop any playback that is happening
		Call RedButton() 'Show red text on rapid playback button
		
		For X = 0 To lstClipboard.Items.Count - 1 'Go through all items from top to bottom, this pastes them back in the reverse order.
			lstXSheet.Items.Insert(lstXSheet.SelectedIndex + 1, VB6.GetItemString(lstClipboard, X))
		Next X
		
		ChangesMade = True 'Log that there has been changes to the X sheet
	End Sub
	
	Private Sub EditorDuplicate1Second()
		Dim X As Integer
		
		'First check that only one frame has been selected in the X sheet
		If lstXSheet.SelectedItems.Count <> 1 Then
			Call MsgBox("Select only 1 frame for duplication", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Sub
		End If
		
		txtRapidText.Text = ReportNotReady 'Inform program that a rapid play is not available
		Call StopAllPlayback() 'Stop any playback that is happening
		Call RedButton() 'Show red text on rapid playback button
		
		For X = 0 To (FrameRate - 2)
			lstXSheet.Items.Insert((lstXSheet.SelectedIndex + 1), VB6.GetItemString(lstXSheet, lstXSheet.SelectedIndex)) 'Duplicate the frame name after the selected position
		Next X
		
		ChangesMade = True 'Log that there has been changes to the X sheet
	End Sub
	
	Private Function ExtractLastFrame() As Boolean 'Extracts the lst frame from the current avi file (used only when exiting editor)
		ExtractLastFrame = False 'Set the funtion to initially return an error
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(SavePath & ProjName & ".avi") = "" Then Exit Function 'If the file doesnt exist, exit the function
		On Error Resume Next 'Try to continue if an error occurs
		FileOpen(10, SavePath & ProjName & ".avi", OpenMode.Binary, OpenAccess.Read) 'Open the avi file for reading
		If GetRefPoints = False Then Exit Function 'Find the start of the movi and index data exit if it doesnt work
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, IndexBlock, idx1Start + ((TotNumFrames - 1) * 16) + 8 + 1) 'Fill the structure with the frame information of the specified frame
		ReDim RawJpegData(IndexBlock.dwSize - 1) 'Redimension the byte array to be able to hold all the jpg data from image file. This clears it also
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, RawJpegData, IndexBlock.dwOffset + 8 + 1) 'This pulls the data from the file to fill the data array
		FileOpen(11, My.Application.Info.DirectoryPath & "\Undo.jpg", OpenMode.Binary) 'Open a new destination File
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(11, RawJpegData) 'Write the raw jpeg data
		FileClose(11) 'Close the file
		FileClose(10)
		ExtractLastFrame = True 'Set the funtion to return a success
	End Function
	Private Function ExtractFrameToBytes(ByRef FrameIDText As String) As Boolean 'Given the line in the xsheet eg B000012, A000695 this function extracts the individual frame
		'and places it in the jpeg byte array
		Dim SourceId As Integer 'The first character of the string, ie A,B,C,D transposed to A = 0 B = 1 C = 2 etc
		Dim FrameIDNumber As Integer 'The frame number extracted from the string
		Dim SourceFileName As String
		
		ExtractFrameToBytes = False
		
		SourceId = CInt(Asc(VB.Left(FrameIDText, 1)) - 65) 'Sets the variable to the first character. A = 0 B = 1 C = 2 etc
		SourceFileName = SourceFileNames(SourceId) 'Extract the source file name from the array
		FrameIDNumber = CInt(Val(Mid(FrameIDText, 2))) 'Extracts the frame id from the string
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(SourceFileName) = "" Then Exit Function 'If the file doesnt exist, exit the function
		On Error Resume Next 'Try to continue if an error occurs
		
		FileOpen(10, SourceFileName, OpenMode.Binary, OpenAccess.Read) 'Open the avi file for reading
		If GetRefPoints = False Then Exit Function 'Find the start of the movi and index data exit if it doesnt work
		If FrameIDNumber = -999 Then FrameIDNumber = TotNumFrames 'If the program sends -999 then assume that it wants the last frame extracted
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, IndexBlock, idx1Start + ((FrameIDNumber - 1) * 16) + 8 + 1) 'Fill the structure with the frame information of the specified frame
		ReDim RawJpegData(IndexBlock.dwSize - 1) 'Redimension the byte array to be able to hold all the jpg data from image file. This clears it also
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, RawJpegData, IndexBlock.dwOffset + 8 + 1) 'This pulls the data from the file to fill the data array
		FileClose(10)
		ExtractFrameToBytes = True
	End Function
	Private Function GetRefPoints() As Boolean
		'This subroutine gets vital points in the avi file, such as where the movi data and index start positions are. It returns the points to variables MoviStart and idx1Start
		'It also does some error checking
		GetRefPoints = False 'Initially tell the function to return an error
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, 112 + 1) 'Get the data that specifies what type of file we have
		If ReadinData1 = &H47504A4D Then 'Check if data contains "MJPG"
			' frmUtilities2.List1.AddItem "This is a motion jpeg file"
		Else
			Call MsgBox("This doesn't appear to be a Motion Jpeg file" & " but contains " & "&H" & Hex(ReadinData1), MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			ErrorCode = 1
			Exit Function
		End If
		
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, 248 + 1) 'Get the data that specifies the start of the junk header
		If ReadinData1 = &H4B4E554A Then 'Check if data contains "JUNK"
			'  frmUtilities2.List1.AddItem "The junk header starts at byte &Hf8"
		Else
			Call MsgBox("I cannot find the Junk Header but found hex value " & "&H" & Hex(ReadinData1), MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			ErrorCode = 2
			Exit Function
		End If
		
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, 252 + 1) 'Get the length of the junk header
		MoviStart = 264 + ReadinData1 'Calculate where "movi" line should be
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, MoviStart + 1) 'Extract data from that line
		If ReadinData1 = &H69766F6D Then 'Check if data contains "movi"
			'   frmUtilities2.List1.AddItem "The movi header starts at byte " & "&H" & Hex$(MoviStart)
		Else
			Call MsgBox("I cannot find the movi header but found hex value " & "&H" & Hex(ReadinData1), MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			ErrorCode = 3
			Exit Function
		End If
		
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, MoviStart - 4 + 1) 'Get the length of the movi data
		idx1Start = MoviStart + ReadinData1 'Calculate where "idx1" line should be
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, idx1Start + 1) 'Extract data from that line
		If ReadinData1 = &H31786469 Then 'Check if data contains "idx1"
			'   frmUtilities2.List1.AddItem "The idx1 header starts at byte " & "&H" & Hex$(idx1Start)
		Else
			Call MsgBox("I cannot find the idx1 header but found hex value " & "&H" & Hex(ReadinData1), MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			ErrorCode = 4
			Exit Function
		End If
		
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, TotNumFrames, 48 + 1) 'Get the total number of frames as specified in the header
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(10, ReadinData1, idx1Start + 4 + 1) 'get the length of the index in bytes
		If TotNumFrames = ReadinData1 / 16 Then 'The each frame in the index occupies 16 bytes, so check it is the correct length
			'  frmUtilities2.List1.AddItem "There are a total of " & TotNumFrames & " frames in the file"
		Else
			Call MsgBox("The index length does not appear to match the number of frames in the header!" & Chr(13) & Chr(10) & "Header says " & TotNumFrames & Chr(13) & Chr(10) & "Index has " & ReadinData1 / 16, MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			ErrorCode = 5
			Exit Function
		End If
		GetRefPoints = True 'All is Ok so tell main program
	End Function
	Private Sub tmrPlay_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrPlay.Tick 'The playback timer used for actioning slow playback only
		
		EditorListPosition = EditorListPosition + 1 'Increment the list position
		If EditorListPosition > lstXSheet.Items.Count - 1 Then
			EditorListPosition = lstXSheet.Items.Count - 1 'Check its not past the end
			If SlowStatus = "Playing" Then
				tmrPlay.Enabled = False 'switch off playback
				tmrEditorFrameRate.Enabled = False
				Call AudioPlayer_Stop() 'Stop the audio track
				SlowStatus = "Stopped"
			End If
			If SlowStatus = "Looping" Then
				EditorListPosition = 0 'Set list position to zero
			End If
		End If
		GetFrameImage() 'Get the frame and display it
		PositionEditorSlider((1)) 'Reposition the frame slider
		Call AudioPlayer_Sync() 'Syncronise the audio player
		
	End Sub
	
	
	Private Sub tmrEditorFrameRate_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrEditorFrameRate.Tick 'Triggered during slow playback to print the fame rate
		If SlowStatus = "Playing" Or SlowStatus = "Looping" Then 'Check we are in slow playback or slow looping
			CurrentFrameFPS = EditorListPosition 'Get the current frame number
			If CInt(CurrentFrameFPS - LastFrameFPS) > FPS Then GoTo DontPrintFPS : 'An error occurs when first starting, so avoid this large value
			txtEditorPlayBackRate.Text = "Playback FPS: " & CurrentFrameFPS - LastFrameFPS 'Print the value
			' Call AudioPlayer_SetRate(CDbl(CDbl(CurrentFrameFPS - LastFrameFPS) / CDbl(FPS)))       'lower the audio playrate to match the frames per second
DontPrintFPS: 
			LastFrameFPS = CurrentFrameFPS 'Store the last frame number for the next time it is called
		End If
	End Sub


End Class